package com.perstorp.cykelapp;

import java.io.IOException;

import com.google.android.gms.internal.pr;

import android.app.Fragment;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ProfileFragment extends Fragment {

	View rootView;
	private String[] gender = { "Kvinna", "Man" };
	// DatabaseHandler dataBase;
	SQLDatabase database;

	public static ProfileFragment newInstance() {
		ProfileFragment fragment = new ProfileFragment();
		return fragment;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater
				.inflate(R.layout.fragment_profile, container, false);

		database = new SQLDatabase(rootView.getContext());
		try {
			database.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
		try {
			database.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
		final Spinner genderSpinner = (Spinner) rootView
				.findViewById(R.id.profileGenderSpinner);
		Button saveProfileBtn = (Button) rootView
				.findViewById(R.id.saveProfileBtn);
		final EditText weightEdTxt = (EditText) rootView
				.findViewById(R.id.profileWeightEdTxt);
		final EditText lengthEdTxt = (EditText) rootView
				.findViewById(R.id.profileLengthEdTxt);
		final EditText ageEdTxt = (EditText) rootView
				.findViewById(R.id.profileAgeEdTxt);

		ArrayAdapter<String> adapter = new MyCustomAdapter(
				rootView.getContext(), R.layout.custom_gender_spinner_layout,
				gender);

		genderSpinner.setAdapter(adapter);

		saveProfileBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int iWeight = Integer
						.parseInt(weightEdTxt.getText().toString());
				int iLength = Integer
						.parseInt(lengthEdTxt.getText().toString());
				int iAge = Integer.parseInt(ageEdTxt.getText().toString());
				String sGender = genderSpinner.getSelectedItem().toString();

				try {
					database.updateProfile(iWeight, iLength, iAge, sGender);
					Toast.makeText(rootView.getContext(),
							"Updated database profile", Toast.LENGTH_SHORT)
							.show();
				} catch (SQLException e) {
					Toast.makeText(rootView.getContext(),
							"Could not updated profile", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

		try {
			String[] profile = database.getProfile();
			if (profile != null) {
				weightEdTxt.setText(profile[1].toString());
				lengthEdTxt.setText(profile[2].toString());
				ageEdTxt.setText(profile[3].toString());
				if (profile[4].equals("Kvinna")) {
					genderSpinner.setSelection(0);
				} else {
					genderSpinner.setSelection(1);
				}
			}
		} catch (Exception e) {
			Toast.makeText(rootView.getContext(), "Could not get profile",
					Toast.LENGTH_SHORT).show();
		}

		return rootView;
	}

	public class MyCustomAdapter extends ArrayAdapter<String> {

		public MyCustomAdapter(Context ctx, int txtViewResourceId,
				String[] objects) {
			super(ctx, txtViewResourceId, objects);
		}

		@Override
		public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
			return getCustomView(position, cnvtView, prnt);
		}

		@Override
		public View getView(int pos, View cnvtView, ViewGroup prnt) {
			return getCustomView(pos, cnvtView, prnt);
		}

		public View getCustomView(int position, View convertView,
				ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater
					.from(rootView.getContext());
			View myCustomSpinner = inflater.inflate(
					R.layout.custom_gender_spinner_layout, parent, false);
			TextView main_text = (TextView) myCustomSpinner
					.findViewById(R.id.customGenderSpinnerTxtV);

			main_text.setText(gender[position]);
			// ImageView left_icon = (ImageView) mySpinner
			// .findViewById(R.id.left_pic);
			// left_icon.setImageResource(total_images[position]);
			return myCustomSpinner;
		}
	}
}
